package com.timesheet.gui.efforttracker.controllers;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;

import com.example.timesheet.uitl.DateUtil;
import com.timesheet.gui.efforttracker.composite.LeaveResultComposite;
import com.timesheet.gui.efforttracker.container.LeaveModelContainer;
import com.timesheet.server.common.models.EffortTrackerDTO;

public class LeaveResultCompositeController implements PropertyChangeListener {
	
	LeaveResultComposite leaveResultComposite = null;
	LeaveModelContainer leaveModelContainer = LeaveModelContainer.getInstance();
	
	public LeaveResultCompositeController(LeaveResultComposite leaveResultComposite) {
		this.leaveResultComposite = leaveResultComposite ;
		init();
		leaveModelContainer.addPropertyChangeListener(this);
	}

	private void init() {
		TableViewer tableViewer = leaveResultComposite.getTableViewer();
		tableViewer.setContentProvider(new LeaveContentProvider());
		tableViewer.setLabelProvider(new LeaveTableLabelProvider());
		tableViewer.setInput(leaveModelContainer.getMyLeaves());
		
	}
	
	private class LeaveContentProvider implements IStructuredContentProvider{

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Object[] getElements(Object input) {
			if(input instanceof List ){
				List<EffortTrackerDTO> list = (List<EffortTrackerDTO>)input ;
				if(list!=null){
					return list.toArray() ;
				}
			}
			return null;
		}
		
	}
	
	private class LeaveTableLabelProvider implements ITableLabelProvider{

		@Override
		public void addListener(ILabelProviderListener arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean isLabelProperty(Object arg0, String arg1) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Image getColumnImage(Object arg0, int arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String getColumnText(Object obj, int index) {
			if(obj instanceof EffortTrackerDTO){
				EffortTrackerDTO dto = (EffortTrackerDTO) obj ;
				if(index == 0){
					return DateUtil.getDateAsString(dto.getDayDate());
				}
				if(index == 1){
					return dto.getEmployeeId();
				}
				if(index == 2){
					return String.valueOf(dto.getHours());
				}
				if(index == 3){
					return dto.getProjectId();
				}
				if(index == 4){
					return dto.getTaskDescription();
				}
				
			}
			return null;
		}
		
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		System.out.println("inside LeaveResultCompositeController");
		this.leaveResultComposite.getTableViewer().setInput(leaveModelContainer.getMyLeaves());
		this.leaveResultComposite.getTableViewer().refresh();	
	}

}
